import {Injectable} from '@angular/core';
import {Subject} from 'rxjs/Subject';
import {Http} from '@angular/http';

import {CartResult} from '../shared/cartResult.interface';
import {CartItem} from '../shared/common.interface';
import {HttpService} from './http.service';

@Injectable()

export class Cart1Service {
  _url = 'http://localhost:3000/carts/';
   result: CartResult = {
    cart: [],
    totalNumber: 0,
    totalPrice: 0,
    selectedTotalNumber: 0,
    selectedTotalPrice: 0,
    allS: true
  };
  cartChanged = new Subject<CartResult>();
   constructor (
     private http: Http,
     private httpService: HttpService
   ) {}

   getCartList () {
     return this.httpService.request(this._url + 'list', 'Get')
       .map(
         (data) => {
           console.log('cartService');
           console.log(data);
           if (data.code === 1) {
             this.result.cart = data.data;
             this.calc();
           }
           return this.result;
         }
       );
   }
  deleteProduct(id: number) {
    return this.httpService.request(this._url + 'delete', 'Get', {goodsId: id});
   }
  addToCart(postData) {
    return this.httpService.request(this._url + 'add', 'Post', postData);
  }
  updateCart(postData) {
    return this.httpService.request(this._url + 'update', 'Post', postData);
  }
  selectedProduct(p, selected: boolean) {
    p.productIsSelected = !selected;
    let a = 0;
    for (const pro of this.result.cart) {
      if (pro.productIsSelected) {
        a += 1;
      }else {
        a += 0;
      }
    }
    if (a === this.result.cart.length) {
      this.result.allS = true;
    }else {
      this.result.allS = false;
    }
    this.calc();
  }
  selectedAll() {
    if (this.result.allS) {
      for (const p of this.result.cart) {
        p.productIsSelected = false;
      }
    }else {
      for (const p of this.result.cart) {
        p.productIsSelected = true;
      }
    }
    this.calc();
  }
   calc() {
     let price = 0;
     let number = 0;
     let selectedPrice = 0;
     let selectedNumber = 0;
     if (this.result.cart) {
       for (const p of this.result.cart) {
         if (p.productIsSelected) {
           selectedPrice += p.quantity * p.price;
           selectedNumber += p.quantity;
         }
         price += p.quantity * p.price;
         number += p.quantity;
       }
       this.result.totalPrice = price;
       this.result.totalNumber = number;
       this.result.selectedTotalPrice = selectedPrice;
       this.result.selectedTotalNumber = selectedNumber;
     }
       // this.cartChanged.next(this.result);
       // localStorage.setItem('cart', JSON.stringify(this.result));
   }
  localGetCart() {
    return this.result;
  }
  localDeleteProduct(id: number) {
    for (let i = 0; i < this.result.cart.length; i++) {
      if (this.result.cart[i].goodsId === id) {
        this.result.cart.splice(i, 1);
      }
    }
    this.calc();
  }
  localAddProduct(product: CartItem) {
    if (this.result['cart']) {
      for (const p of this.result['cart']) {
        if (p['goodsId'] === product['goodsId']) {
          p['quantity'] += product['quantity'];
          this.calc();
          return;
        }
      }
    }
    product.productIsSelected = true;
    this.result.cart.push(product);
    this.calc();
  }
  localChangeQuantity(quantity: number, id: number) {
     if (quantity <= 0) {
       this.localDeleteProduct(id);
     } else {
       for (let i = 0; i < this.result.cart.length; i++) {
         if (this.result.cart[i].goodsId === id) {
           this.result.cart[i].quantity = quantity;
         }
       }
     }
    this.calc();
  }
}
